#!/usr/bin/python
# -*- coding:UTF8 -*-
import requests
from lxml import etree
import os

# 先判断路径是否存在，不存在就创建
if not os.path.exists('第一季/测试数据/海报'):
    os.makedirs('第一季/测试数据/海报')
# 获取网址
page_url = 'https://www.jkl.com.cn/phoLis.aspx'
user_agent = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'}
for current in range(1, 4):
    params = {
        'current': current
    }
    r = requests.get(url=page_url, headers=user_agent, params=params).text
    # 解析数据
    analytical_data = etree.HTML(r)
    pages = analytical_data.xpath('//div[@class="proLis"]//@src')
    for page in pages:
        page_url1 = 'https://www.jkl.com.cn' + page
        r = requests.get(url=page_url1, headers=user_agent).content
        page_name = page_url1.split('/')[-1]
        page_path = './测试数据/海报/' + page_name
        # 写入数据
        with open(page_path, 'wb') as file:
            file.write(r)
            print(page_name, '下载成功！！！')
